﻿using System.Threading;
using Microsoft.Win32;

namespace WpfAdvControls.Dialogs
{
    public class OpenFileInvoker
    {
        private readonly Thread InvokeThread;
        public OpenFileDialog dialog;
        public bool? InvokeResult;

        public OpenFileInvoker()
        {
            InvokeThread = new Thread(InvokeMethod);
            InvokeThread.SetApartmentState(ApartmentState.STA);
            InvokeResult = null;
        }

        public string FileName { get; set; }


        public string InitialDirectory { get; set; }

        public bool? Invoke()
        {
            InvokeThread.Start();
            InvokeThread.Join();
            return InvokeResult;
        }

        private void InvokeMethod()
        {
            dialog = new OpenFileDialog();
            dialog.InitialDirectory = InitialDirectory;
            InvokeResult = dialog.ShowDialog();
            FileName = dialog.FileName;
        }
    }
}